package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import entity.Properties;
import entity.PsLineInfo;
import entity.PsLineInfo;

@Repository("lineDao")
public class LineDao<K, V> extends HibernateDaoSupport{
	
	public List<Properties<K, V>> findDistinctLineName(){
		List<Properties<K, V>> list=new ArrayList<Properties<K, V>>();
		List l=this.getHibernateTemplate().find("select distinct id,linename from PsLineInfo");
		for (Object o : l) {		
			Object[] temp=(Object[])o;
			list.add(new Properties(temp[0],temp[1]));
		}
		return list;
	}
	public List<Properties<K, V>> getLineRunState(){
		List<Properties<K, V>> list=new ArrayList<Properties<K, V>>();
		List l=this.getHibernateTemplate().find("select distinct runningstatus,runningstatusname from PsLineInfo");
		for (Object o : l) {		
			Object[] temp=(Object[])o;
			list.add(new Properties(temp[0],temp[1]));
		}
		return list;
	}
	public List<PsLineInfo> selectAllLine(int page,int rows,DetachedCriteria searchDc){
		return (List<PsLineInfo>)this.getHibernateTemplate().findByCriteria(searchDc, (page-1)*rows, rows);
	}
	public int count(DetachedCriteria searchDc){
		searchDc.setProjection(Projections.rowCount());
		List<Long> count=(List<Long>)this.getHibernateTemplate().findByCriteria(searchDc);
		return count.get(0).intValue();
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void changeIsStart(PsLineInfo psLine) throws Exception{
		PsLineInfo p=this.getHibernateTemplate().get(PsLineInfo.class, psLine.getId());	
		if(p.getIsstart()==1){
			p.setIsstart((byte) -1);
		}else{
			p.setIsstart((byte) 1);
		}		
		
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void deleteLine(final PsLineInfo psLine)  throws Exception{
		this.getHibernateTemplate().delete(psLine);		
		
	}
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void addUpdateLine(PsLineInfo psLine)  throws Exception{
		if(psLine.getId()!=null){
			PsLineInfo p=this.getHibernateTemplate().get(PsLineInfo.class, psLine.getId());
			BeanUtils.copyProperties(psLine, p);
		}else{
			this.getHibernateTemplate().save(psLine);
		}
		
		
	}
	public PsLineInfo findLine(PsLineInfo psLine){
		return ((List<PsLineInfo>)this.getHibernateTemplate().find("from PsLineInfo where id=?",
				psLine.getId())).get(0);
		
	}
	public boolean findLineCode(PsLineInfo psLine){
		return ((List<PsLineInfo>)this.getHibernateTemplate().find("from PsLineInfo where linecode=?",
				psLine.getLinecode())).size()>0?true:false;
	}
}
